
## 安装 k8s

> https://docs.rancher.cn/docs/rke/os/_index

### 禁止swap分区


sed -i "s/\/swap.img/# \/swap.img/g" /etc/fstab

cat /etc/fstab

sudo swapoff -a

### ssh 免密远程

useradd docker-00


mkdir -p /home/docker-00 && chmod -R 777 /home/docker-00


passwd docker-00 << "EOF"
docker-00-!QAZ@WSX
docker-00-!QAZ@WSX
EOF


usermod -aG docker docker-00

su docker-00

ssh-keygen

ssh-copy-id docker-00@cardone-1

ssh-copy-id docker-00@cardone-1

ssh-copy-id docker-00@cardone-2

ssh docker-00@cardone-1

chmod g-w /home/docker-00
chmod 700 /home/docker-00/.ssh 
chmod 600 /home/docker-00/.ssh/authorized_keys

sudo vi /home/docker-00/.ssh/authorized_keys

sudo scp /home/docker-00/.ssh/authorized_keys docker-00@15.142.2.32:/home/docker-00/.ssh/authorized_keys

for module in br_netfilter ip6_udp_tunnel ip_set ip_set_hash_ip ip_set_hash_net iptable_filter iptable_nat iptable_mangle iptable_raw nf_conntrack_netlink nf_conntrack nf_conntrack_ipv4   nf_defrag_ipv4 nf_nat nf_nat_ipv4 nf_nat_masquerade_ipv4 nfnetlink udp_tunnel veth vxlan x_tables xt_addrtype xt_conntrack xt_comment xt_mark xt_multiport xt_nat xt_recent xt_set  xt_statistic xt_tcpudp;
    do
      if ! lsmod | grep -q $module; then
        echo "module $module is not present";
      fi;
    done





(
cat << 'EOF'
#########################################################################
# File Name: module-check+install.sh
# Created Time: Wed 16 Sep 2020 02:20:32 PM CST
#########################################################################
#!/bin/zsh
for module in br_netfilter ip6_udp_tunnel ip_set ip_set_hash_ip ip_set_hash_net iptable_filter iptable_nat iptable_mangle iptable_raw nf_conntrack_netlink nf_conntrack nf_conntrack_ipv4   nf_defrag_ipv4 nf_nat nf_nat_ipv4 nf_nat_masquerade_ipv4 nfnetlink udp_tunnel veth vxlan x_tables xt_addrtype xt_conntrack xt_comment xt_mark xt_multiport xt_nat xt_recent xt_set  xt_statistic xt_tcpudp; do
    if ! lsmod | grep -q $module; then
        echo "module $module is not present, try to install...";
                modprobe $module
                if [ $? -eq 0 ]; then
                        echo -e "\033[32;1mSuccessfully installed $module!\033[0m"
                else
                        echo -e "\033[31;1mInstall $module failed!!!\033[0m"
                fi
        fi;
done
EOF
) >/home/cardone/module.sh

chmod u+x /home/cardone/module.sh

/home/cardone/module.sh

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target


(
cat << 'EOF'
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
) >/etc/sysctl.d/k8s.conf

sudo sysctl --system

sudo apt-get update && sudo apt-get install -y ca-certificates curl software-properties-common apt-transport-https curl

curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 

(
cat << 'EOF'
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
) >/etc/apt/sources.list.d/kubernetes.list


sudo apt-get update

# 查看可安装版本
# apt-cache madison kubelet
# apt-cache madison kubeadm


#sudo apt-get remove kubelet kubeadm kubectl

apt-get install -y kubelet=1.18.0-00 kubeadm=1.18.0-00 kubectl=1.18.0-00

sudo apt-mark hold kubelet kubeadm kubectl

sudo systemctl enable kubelet && sudo systemctl start kubelet

# kubeadm config images list --kubernetes-version=v1.18.0

kubeadm init --v=5 --apiserver-advertise-address=15.142.2.30 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16


# kubeadm init --config kubeadm-config.yaml --upload-certs --v=5

# kubeadm init --kubernetes-version=v1.18.0 --apiserver-advertise-address 15.142.2.31 --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=all



To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 15.142.2.30:6443 --token xer1g6.e2x2c4pfemljjnjq \
    --discovery-token-ca-cert-hash sha256:c831fce4dc59b8040d8002d06bd70fab260932412f8d64feab44ff98a058a66f


kubectl get nodes



kubectl apply -f kube-flannel.yml

kubectl get pods -n kube-system


### test


kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc

